/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __QS_TransactionDownloadWidget_H__
#define __QS_TransactionDownloadWidget_H__

#include "UIWidgets.h"

#include <QtGui/QCompleter>
#include "TransactionDownloader.h"
#include "TransactionViewDialog.h"
#include "DiagramDialog.h"

namespace QSThink
{
	
	//
	class TransactionDownloadWidget : 
		public QWidget,
		public TransactionDownloadListener
	{
		Q_OBJECT

	public:
		TransactionDownloadWidget(QWidget *parent=0,Qt::WindowFlags flag=0);
		virtual ~TransactionDownloadWidget();
		void setInputCompleter();
	protected:
		void showDayTransDialog(Transaction1Day* tran);
		void showDayTransGraphicsDialog( QString symbo, QDate date );


		void loadDownloadedItems(QString symbol);

		void OnDayTransactionDownloaded(Transaction1Day* trans);
		void OnTaskFinished(TransactionDownloadTask* task);

		void addDayTransactionItem( Transaction1Day* trans );

		void persit();

		void contextMenuEvent (QContextMenuEvent * evt);

	protected:
		TransactionViewDialog* _dataViewDialog;
		DiagramDialog* _dataDiagramDialog;
		Ui::TransactionDownloadUI* _ui;
		QCompleter* _completer;
		QMenu* _contextMenu;
		
		QString _symbolEdit;//edit

		typedef std::map<QListWidgetItem*,QString> ItemToSymbolMap;
		typedef std::map<QString,QListWidgetItem*> SymbolToItemMap;

		ItemToSymbolMap _itemToSymbols;
		SymbolToItemMap _symbolToItems;

		std::vector<QString> _symbols;

		QDate _startDate;
		QDate _endDate;
		QString _symbolSelect;
	protected slots:
		void symbolSelected(const QModelIndex &index);
		void symbolChanged( const QString & symbol );
		void symbolEdited( const QString &symbol );
		void addSymbol();
		void openDataTable();
		void OnItemDoubleClicked( QListWidgetItem* item );
		void OnDownItemDoubleClicked( QListWidgetItem* item );

		
		void downloadButtonClicked();
		void loadDownloadTask();
		void dateStartChanged( const QDate & date );
		void dateEndChanged( const QDate & date );

	};
}
#endif
